Visualization

SNA Week 6

Matt Pietryka

Much of the material for this lecture has been taken or modified from Katherine Ognyanova’ 2016 Political Networks workshop on network visualization. For her materials, go here: http://kateto.net/polnet2016

Why we visualize networks

Basic visualization options

Visualization norms

Colors in R plots

Colors in R plots: Basics

par(mar = c(0, 0, 0, 0))
x <- 1:10
plot(x = x, y = rep(5, 10), 
     pch=19, # SHAPE
     cex=3,  # SIZE
     col="dark red" # COLOR
     )
points(x = x, y = rep(6, 10), pch=19, cex=3, col="557799")
points(x = x, y = rep(4, 10), pch=19, cex=3, col=rgb(.25, .5, .3))

Set opacity with alpha

par(mar = c(0, 0, 0, 0))
alpha_vals <- seq(0, 1, by = 0.2)
plot(x = 1:6, y = rep(5,6), 
     pch = 19, 
     cex = 20, 
     col = rgb(.25, .5, .3, alpha = alpha_vals), 
     xlim = c(0,7)) 
text(x = 1:6, y = rep(4, 6), labels = alpha_vals,  cex = 2)

Set alpha for hex colors, change background color

par(mar = c(0, 0, 0, 0))                                # MARGINS
par(bg = "gray40")                                      # BACKGROUND COL.
col.tr <- grDevices::adjustcolor("557799", alpha = 0.4) # ADD ALPHA
plot(x = 1:6, y = rep(5,6), 
     pch = 19, cex = 20, col= col.tr, xlim = c(0,7)) 

Can view all predefined colors

colors()                          
##   [1] "white"                "aliceblue"            "antiquewhite"        
##   [4] "antiquewhite1"        "antiquewhite2"        "antiquewhite3"       
##   [7] "antiquewhite4"        "aquamarine"           "aquamarine1"         
##  [10] "aquamarine2"          "aquamarine3"          "aquamarine4"         
##  [13] "azure"                "azure1"               "azure2"              
##  [16] "azure3"               "azure4"               "beige"               
##  [19] "bisque"               "bisque1"              "bisque2"             
##  [22] "bisque3"              "bisque4"              "black"               
##  [25] "blanchedalmond"       "blue"                 "blue1"               
##  [28] "blue2"                "blue3"                "blue4"               
##  [31] "blueviolet"           "brown"                "brown1"              
##  [34] "brown2"               "brown3"               "brown4"              
##  [37] "burlywood"            "burlywood1"           "burlywood2"          
##  [40] "burlywood3"           "burlywood4"           "cadetblue"           
##  [43] "cadetblue1"           "cadetblue2"           "cadetblue3"          
##  [46] "cadetblue4"           "chartreuse"           "chartreuse1"         
##  [49] "chartreuse2"          "chartreuse3"          "chartreuse4"         
##  [52] "chocolate"            "chocolate1"           "chocolate2"          
##  [55] "chocolate3"           "chocolate4"           "coral"               
##  [58] "coral1"               "coral2"               "coral3"              
##  [61] "coral4"               "cornflowerblue"       "cornsilk"            
##  [64] "cornsilk1"            "cornsilk2"            "cornsilk3"           
##  [67] "cornsilk4"            "cyan"                 "cyan1"               
##  [70] "cyan2"                "cyan3"                "cyan4"               
##  [73] "darkblue"             "darkcyan"             "darkgoldenrod"       
##  [76] "darkgoldenrod1"       "darkgoldenrod2"       "darkgoldenrod3"      
##  [79] "darkgoldenrod4"       "darkgray"             "darkgreen"           
##  [82] "darkgrey"             "darkkhaki"            "darkmagenta"         
##  [85] "darkolivegreen"       "darkolivegreen1"      "darkolivegreen2"     
##  [88] "darkolivegreen3"      "darkolivegreen4"      "darkorange"          
##  [91] "darkorange1"          "darkorange2"          "darkorange3"         
##  [94] "darkorange4"          "darkorchid"           "darkorchid1"         
##  [97] "darkorchid2"          "darkorchid3"          "darkorchid4"         
## [100] "darkred"              "darksalmon"           "darkseagreen"        
## [103] "darkseagreen1"        "darkseagreen2"        "darkseagreen3"       
## [106] "darkseagreen4"        "darkslateblue"        "darkslategray"       
## [109] "darkslategray1"       "darkslategray2"       "darkslategray3"      
## [112] "darkslategray4"       "darkslategrey"        "darkturquoise"       
## [115] "darkviolet"           "deeppink"             "deeppink1"           
## [118] "deeppink2"            "deeppink3"            "deeppink4"           
## [121] "deepskyblue"          "deepskyblue1"         "deepskyblue2"        
## [124] "deepskyblue3"         "deepskyblue4"         "dimgray"             
## [127] "dimgrey"              "dodgerblue"           "dodgerblue1"         
## [130] "dodgerblue2"          "dodgerblue3"          "dodgerblue4"         
## [133] "firebrick"            "firebrick1"           "firebrick2"          
## [136] "firebrick3"           "firebrick4"           "floralwhite"         
## [139] "forestgreen"          "gainsboro"            "ghostwhite"          
## [142] "gold"                 "gold1"                "gold2"               
## [145] "gold3"                "gold4"                "goldenrod"           
## [148] "goldenrod1"           "goldenrod2"           "goldenrod3"          
## [151] "goldenrod4"           "gray"                 "gray0"               
## [154] "gray1"                "gray2"                "gray3"               
## [157] "gray4"                "gray5"                "gray6"               
## [160] "gray7"                "gray8"                "gray9"               
## [163] "gray10"               "gray11"               "gray12"              
## [166] "gray13"               "gray14"               "gray15"              
## [169] "gray16"               "gray17"               "gray18"              
## [172] "gray19"               "gray20"               "gray21"              
## [175] "gray22"               "gray23"               "gray24"              
## [178] "gray25"               "gray26"               "gray27"              
## [181] "gray28"               "gray29"               "gray30"              
## [184] "gray31"               "gray32"               "gray33"              
## [187] "gray34"               "gray35"               "gray36"              
## [190] "gray37"               "gray38"               "gray39"              
## [193] "gray40"               "gray41"               "gray42"              
## [196] "gray43"               "gray44"               "gray45"              
## [199] "gray46"               "gray47"               "gray48"              
## [202] "gray49"               "gray50"               "gray51"              
## [205] "gray52"               "gray53"               "gray54"              
## [208] "gray55"               "gray56"               "gray57"              
## [211] "gray58"               "gray59"               "gray60"              
## [214] "gray61"               "gray62"               "gray63"              
## [217] "gray64"               "gray65"               "gray66"              
## [220] "gray67"               "gray68"               "gray69"              
## [223] "gray70"               "gray71"               "gray72"              
## [226] "gray73"               "gray74"               "gray75"              
## [229] "gray76"               "gray77"               "gray78"              
## [232] "gray79"               "gray80"               "gray81"              
## [235] "gray82"               "gray83"               "gray84"              
## [238] "gray85"               "gray86"               "gray87"              
## [241] "gray88"               "gray89"               "gray90"              
## [244] "gray91"               "gray92"               "gray93"              
## [247] "gray94"               "gray95"               "gray96"              
## [250] "gray97"               "gray98"               "gray99"              
## [253] "gray100"              "green"                "green1"              
## [256] "green2"               "green3"               "green4"              
## [259] "greenyellow"          "grey"                 "grey0"               
## [262] "grey1"                "grey2"                "grey3"               
## [265] "grey4"                "grey5"                "grey6"               
## [268] "grey7"                "grey8"                "grey9"               
## [271] "grey10"               "grey11"               "grey12"              
## [274] "grey13"               "grey14"               "grey15"              
## [277] "grey16"               "grey17"               "grey18"              
## [280] "grey19"               "grey20"               "grey21"              
## [283] "grey22"               "grey23"               "grey24"              
## [286] "grey25"               "grey26"               "grey27"              
## [289] "grey28"               "grey29"               "grey30"              
## [292] "grey31"               "grey32"               "grey33"              
## [295] "grey34"               "grey35"               "grey36"              
## [298] "grey37"               "grey38"               "grey39"              
## [301] "grey40"               "grey41"               "grey42"              
## [304] "grey43"               "grey44"               "grey45"              
## [307] "grey46"               "grey47"               "grey48"              
## [310] "grey49"               "grey50"               "grey51"              
## [313] "grey52"               "grey53"               "grey54"              
## [316] "grey55"               "grey56"               "grey57"              
## [319] "grey58"               "grey59"               "grey60"              
## [322] "grey61"               "grey62"               "grey63"              
## [325] "grey64"               "grey65"               "grey66"              
## [328] "grey67"               "grey68"               "grey69"              
## [331] "grey70"               "grey71"               "grey72"              
## [334] "grey73"               "grey74"               "grey75"              
## [337] "grey76"               "grey77"               "grey78"              
## [340] "grey79"               "grey80"               "grey81"              
## [343] "grey82"               "grey83"               "grey84"              
## [346] "grey85"               "grey86"               "grey87"              
## [349] "grey88"               "grey89"               "grey90"              
## [352] "grey91"               "grey92"               "grey93"              
## [355] "grey94"               "grey95"               "grey96"              
## [358] "grey97"               "grey98"               "grey99"              
## [361] "grey100"              "honeydew"             "honeydew1"           
## [364] "honeydew2"            "honeydew3"            "honeydew4"           
## [367] "hotpink"              "hotpink1"             "hotpink2"            
## [370] "hotpink3"             "hotpink4"             "indianred"           
## [373] "indianred1"           "indianred2"           "indianred3"          
## [376] "indianred4"           "ivory"                "ivory1"              
## [379] "ivory2"               "ivory3"               "ivory4"              
## [382] "khaki"                "khaki1"               "khaki2"              
## [385] "khaki3"               "khaki4"               "lavender"            
## [388] "lavenderblush"        "lavenderblush1"       "lavenderblush2"      
## [391] "lavenderblush3"       "lavenderblush4"       "lawngreen"           
## [394] "lemonchiffon"         "lemonchiffon1"        "lemonchiffon2"       
## [397] "lemonchiffon3"        "lemonchiffon4"        "lightblue"           
## [400] "lightblue1"           "lightblue2"           "lightblue3"          
## [403] "lightblue4"           "lightcoral"           "lightcyan"           
## [406] "lightcyan1"           "lightcyan2"           "lightcyan3"          
## [409] "lightcyan4"           "lightgoldenrod"       "lightgoldenrod1"     
## [412] "lightgoldenrod2"      "lightgoldenrod3"      "lightgoldenrod4"     
## [415] "lightgoldenrodyellow" "lightgray"            "lightgreen"          
## [418] "lightgrey"            "lightpink"            "lightpink1"          
## [421] "lightpink2"           "lightpink3"           "lightpink4"          
## [424] "lightsalmon"          "lightsalmon1"         "lightsalmon2"        
## [427] "lightsalmon3"         "lightsalmon4"         "lightseagreen"       
## [430] "lightskyblue"         "lightskyblue1"        "lightskyblue2"       
## [433] "lightskyblue3"        "lightskyblue4"        "lightslateblue"      
## [436] "lightslategray"       "lightslategrey"       "lightsteelblue"      
## [439] "lightsteelblue1"      "lightsteelblue2"      "lightsteelblue3"     
## [442] "lightsteelblue4"      "lightyellow"          "lightyellow1"        
## [445] "lightyellow2"         "lightyellow3"         "lightyellow4"        
## [448] "limegreen"            "linen"                "magenta"             
## [451] "magenta1"             "magenta2"             "magenta3"            
## [454] "magenta4"             "maroon"               "maroon1"             
## [457] "maroon2"              "maroon3"              "maroon4"             
## [460] "mediumaquamarine"     "mediumblue"           "mediumorchid"        
## [463] "mediumorchid1"        "mediumorchid2"        "mediumorchid3"       
## [466] "mediumorchid4"        "mediumpurple"         "mediumpurple1"       
## [469] "mediumpurple2"        "mediumpurple3"        "mediumpurple4"       
## [472] "mediumseagreen"       "mediumslateblue"      "mediumspringgreen"   
## [475] "mediumturquoise"      "mediumvioletred"      "midnightblue"        
## [478] "mintcream"            "mistyrose"            "mistyrose1"          
## [481] "mistyrose2"           "mistyrose3"           "mistyrose4"          
## [484] "moccasin"             "navajowhite"          "navajowhite1"        
## [487] "navajowhite2"         "navajowhite3"         "navajowhite4"        
## [490] "navy"                 "navyblue"             "oldlace"             
## [493] "olivedrab"            "olivedrab1"           "olivedrab2"          
## [496] "olivedrab3"           "olivedrab4"           "orange"              
## [499] "orange1"              "orange2"              "orange3"             
## [502] "orange4"              "orangered"            "orangered1"          
## [505] "orangered2"           "orangered3"           "orangered4"          
## [508] "orchid"               "orchid1"              "orchid2"             
## [511] "orchid3"              "orchid4"              "palegoldenrod"       
## [514] "palegreen"            "palegreen1"           "palegreen2"          
## [517] "palegreen3"           "palegreen4"           "paleturquoise"       
## [520] "paleturquoise1"       "paleturquoise2"       "paleturquoise3"      
## [523] "paleturquoise4"       "palevioletred"        "palevioletred1"      
## [526] "palevioletred2"       "palevioletred3"       "palevioletred4"      
## [529] "papayawhip"           "peachpuff"            "peachpuff1"          
## [532] "peachpuff2"           "peachpuff3"           "peachpuff4"          
## [535] "peru"                 "pink"                 "pink1"               
## [538] "pink2"                "pink3"                "pink4"               
## [541] "plum"                 "plum1"                "plum2"               
## [544] "plum3"                "plum4"                "powderblue"          
## [547] "purple"               "purple1"              "purple2"             
## [550] "purple3"              "purple4"              "red"                 
## [553] "red1"                 "red2"                 "red3"                
## [556] "red4"                 "rosybrown"            "rosybrown1"          
## [559] "rosybrown2"           "rosybrown3"           "rosybrown4"          
## [562] "royalblue"            "royalblue1"           "royalblue2"          
## [565] "royalblue3"           "royalblue4"           "saddlebrown"         
## [568] "salmon"               "salmon1"              "salmon2"             
## [571] "salmon3"              "salmon4"              "sandybrown"          
## [574] "seagreen"             "seagreen1"            "seagreen2"           
## [577] "seagreen3"            "seagreen4"            "seashell"            
## [580] "seashell1"            "seashell2"            "seashell3"           
## [583] "seashell4"            "sienna"               "sienna1"             
## [586] "sienna2"              "sienna3"              "sienna4"             
## [589] "skyblue"              "skyblue1"             "skyblue2"            
## [592] "skyblue3"             "skyblue4"             "slateblue"           
## [595] "slateblue1"           "slateblue2"           "slateblue3"          
## [598] "slateblue4"           "slategray"            "slategray1"          
## [601] "slategray2"           "slategray3"           "slategray4"          
## [604] "slategrey"            "snow"                 "snow1"               
## [607] "snow2"                "snow3"                "snow4"               
## [610] "springgreen"          "springgreen1"         "springgreen2"        
## [613] "springgreen3"         "springgreen4"         "steelblue"           
## [616] "steelblue1"           "steelblue2"           "steelblue3"          
## [619] "steelblue4"           "tan"                  "tan1"                
## [622] "tan2"                 "tan3"                 "tan4"                
## [625] "thistle"              "thistle1"             "thistle2"            
## [628] "thistle3"             "thistle4"             "tomato"              
## [631] "tomato1"              "tomato2"              "tomato3"             
## [634] "tomato4"              "turquoise"            "turquoise1"          
## [637] "turquoise2"           "turquoise3"           "turquoise4"          
## [640] "violet"               "violetred"            "violetred1"          
## [643] "violetred2"           "violetred3"           "violetred4"          
## [646] "wheat"                "wheat1"               "wheat2"              
## [649] "wheat3"               "wheat4"               "whitesmoke"          
## [652] "yellow"               "yellow1"              "yellow2"             
## [655] "yellow3"              "yellow4"              "yellowgreen"

Can search predefined colors

grep("blue", colors(), value = TRUE)  
##  [1] "aliceblue"       "blue"            "blue1"          
##  [4] "blue2"           "blue3"           "blue4"          
##  [7] "blueviolet"      "cadetblue"       "cadetblue1"     
## [10] "cadetblue2"      "cadetblue3"      "cadetblue4"     
## [13] "cornflowerblue"  "darkblue"        "darkslateblue"  
## [16] "deepskyblue"     "deepskyblue1"    "deepskyblue2"   
## [19] "deepskyblue3"    "deepskyblue4"    "dodgerblue"     
## [22] "dodgerblue1"     "dodgerblue2"     "dodgerblue3"    
## [25] "dodgerblue4"     "lightblue"       "lightblue1"     
## [28] "lightblue2"      "lightblue3"      "lightblue4"     
## [31] "lightskyblue"    "lightskyblue1"   "lightskyblue2"  
## [34] "lightskyblue3"   "lightskyblue4"   "lightslateblue" 
## [37] "lightsteelblue"  "lightsteelblue1" "lightsteelblue2"
## [40] "lightsteelblue3" "lightsteelblue4" "mediumblue"     
## [43] "mediumslateblue" "midnightblue"    "navyblue"       
## [46] "powderblue"      "royalblue"       "royalblue1"     
## [49] "royalblue2"      "royalblue3"      "royalblue4"     
## [52] "skyblue"         "skyblue1"        "skyblue2"       
## [55] "skyblue3"        "skyblue4"        "slateblue"      
## [58] "slateblue1"      "slateblue2"      "slateblue3"     
## [61] "slateblue4"      "steelblue"       "steelblue1"     
## [64] "steelblue2"      "steelblue3"      "steelblue4"

Can gernerate palettes from predefined sets

pal1 <- heat.colors(5, alpha = 1)   # the 'heat' palette, opaque
pal2 <- rainbow(5, alpha = .5)      # the 'rainbow' palette,  transparent
par(mar = c(0, 0, 0, 0))            # MARGINS
par(mfrow = c(1, 2))                # 1 x 2 WINDOW
plot(x=1:10, y=1:10, pch=19, cex=8, col=pal1, axes=FALSE )
plot(x=1:10, y=1:10, pch=19, cex=8, col=pal2, axes=FALSE)

Can gernerate new palettes

# CREATE NEW FUNCTION TO GENERATE COLORS FROM RED TO WHITE TO BLUE
gen_pal_rb <- colorRampPalette(c("red", "blue")) 
pal3 <- gen_pal_rb(10)              # GENERATE 10 COLORS
# CREATE NEW FUNCTION TO GENERATE COLORS FROM RED TO WHITE TO BLUE
gen_pal_rwb <- colorRampPalette(c("red", "white", "blue")) 
pal4 <- gen_pal_rwb(10)             # GENERATE 10 COLORS

par(mar = c(0, 0, 0, 0))            # MARGINS
par(mfrow = c(1, 2))                # 1 x 2 WINDOW
plot(x=1:10, y=1:10, pch=19, cex=8, col=pal3, axes=FALSE )
plot(x=1:10, y=1:10, pch=19, cex=8, col=pal4, axes=FALSE )

Use ‘RColorBrewer’ package to find good palettes

library('RColorBrewer')
display.brewer.all()

Use RColorBrewer package to find good palettes

par(mar = c(0, 0, 0, 0))            # MARGINS
display.brewer.pal(8, "Set3")

display.brewer.pal(8, "Spectral")

display.brewer.pal(8, "Blues")

Use RColorBrewer package to find good palettes

par(mfrow = c(1, 2))                # 1 x 2 WINDOW
par(mar = c(0, 0, 0, 0))            # MARGINS
pal5 <- brewer.pal(10, "Set3")      # CREATE PALETTE
plot(x=10:1, y=10:1, pch=19, cex=10, col= pal5 , axes=FALSE)
plot(x=10:1, y=10:1, pch=19, cex=10, col= rev(pal5) , axes=FALSE)

Or use http://paletton.com to find good palettes

Plotting in igraph

Preamble

Data from example can be found here: http://deim.urv.cat/~manlio.dedomenico/data.php

set.seed(42)       # SET SEED FOR RANDOM GENERATOR
# ----------------- LOAD PACKAGES ---------------- #
library(tidyverse) # MANY CONVENIENCE FUNCTIONS
library(igraph)    # NETWORK VISUALIZATION

# ----------------- FILE PATHS ---------------- #
path <- "C:/gdrive/Classes/Classes - Taught/"
pjct <- "SNA - Social Network Analysis/Lectures/SNA7 - Visualization/"
path_pjct <- paste0(path, pjct)

Read in Node Data

node_raw <- paste0(path_pjct, "Data/Source/Lazega-Law-Firm_nodes.txt") %>%
  read_delim(delim = " ")  %>%
  mutate(woman = ifelse(nodeGender == 2, 1, 0))  %>%
  mutate(partner = ifelse(nodeStatus == 1, 1, 0))

node_raw %>% head()
## # A tibble: 6 × 10
##   nodeID nodeStatus nodeGender nodeOffice nodeSeniority nodeAge
##    <int>      <int>      <int>      <int>         <chr>   <int>
## 1      1          1          1          1            31      64
## 2      2          1          1          1            32      62
## 3      3          1          1          2            13      67
## 4      4          1          1          1            31      59
## 5      5          1          1          2            31      59
## 6      6          1          1          2            29      55
## # ... with 4 more variables: nodePractice <int>, nodeLawSchool <int>,
## #   woman <dbl>, partner <dbl>

Read in Edge Data

edge_labs <- paste0(path_pjct,
                    "Data/Source/Lazega-Law-Firm_layers.txt") %>%
  read_delim(delim = " ")
edge_raw <- paste0(path_pjct,
                   "Data/Source/Lazega-Law-Firm_multiplex.edges") %>%
  read_delim(delim = " ", col_names = FALSE)

edge_advice <- edge_raw  %>% filter(X1 == 1)  %>% select(X2, X3)

edge_advice  %>% head()
## # A tibble: 6 × 2
##      X2    X3
##   <int> <int>
## 1     1     2
## 2     1    17
## 3     1    20
## 4     2     1
## 5     2     6
## 6     2    17

CREATE IGRAPH NETWORK

# CREATE IGRAPH NETWORK
advice_net <- graph_from_data_frame(d = edge_advice,    # EDGE LIST
                                   vertices = node_raw, # NODE ATTRIBUTES
                                   directed = TRUE      # DIRECTED EDGES
                                   )
par(mar = c(0, 0, 0, 0))
plot(advice_net)

Important igraph plotting parameters: Nodes

Call ?igraph.plotting to see all plotting parameters

Command Description
vertex.color node color
vertex.frame.color node border color
vertex.shape node shape
vertex.size node size
vertex.size2 node size, second dim (i.e., for rectangle)
vertex.label character vector of node labels
vertex.label.family Font
vertex.label.font Font: 1 = plain, 2 bold, 3, italic, 4 bold italic, 5 symbol
vertex.label.cex font size

Important igraph plotting parameters: Edges

Command Description
edge.color Edge color
edge.width Edge width, defaults to 1
edge.arrow.size Arrow size, defaults to 1
edge.arrow.width Arrow width, defaults to 1
edge.lty Line type, could be 0 or “blank”, 1 or “solid”, 2 or “dashed”, 3 or “dotted”, 4 or “dotdash”, 5 or “longdash”, 6 or “twodash”
edge.label Character vector used to label edges
edge.label.family Font family of the label (e.g.“sans”)
edge.label.font Font: 1 = plain, 2 bold, 3, italic, 4 bold italic, 5 symbol
edge.label.cex Font size for edge labels

Can specify node & edge options in the plot() function

- curved edges allow you to see multiple links between two nodes

par(mar = c(0, 0, 0, 0))
plot(advice_net, 
     edge.arrow.size = .4, # reduce arrow size
     edge.curved = .1      # curved edges 
     ) 

par(mar = c(0, 0, 0, 0))
plot(advice_net, edge.arrow.size=.4, edge.color="orange",
     vertex.color="orange", vertex.frame.color="orange",
     vertex.label=NA # NO NODE LABELS
     ) 

Can specify node & edge options by adding them to the igraph object

# Generate colors based on law school:
colrs <- c("tomato", "gold", "gray50" )
V(advice_net)$color <- colrs[V(advice_net)$nodeLawSchool]

# Compute node degrees (#links) and use that to set node size:
deg <- degree(advice_net, mode="all")
V(advice_net)$size <- deg * 0.5

# The labels are currently node IDs.
# Setting them to NA will render no labels:
V(advice_net)$label <- NA

#change arrow size and edge color:
E(advice_net)$arrow.size <- .4
E(advice_net)$edge.color <- "gray80"

Can specify node & edge options by adding them to the igraph object

par(mar = c(0, 0, 0, 0))
plot(advice_net) 

Can specify node & edge options by adding them to the igraph object

par(mar = c(0, 0, 0, 0))
plot(advice_net) 
legend(x=0.51, y=-.2, c("Harvard/Yale","UConn", "Other"), pch=21,
       col="#777777", pt.bg=colrs, pt.cex=2, cex=.8, bty="n", ncol=1)

Color the edges of the graph based on their source node color

# We can get the starting and ending nodes for each edge with the `ends()` function
edge.start <- ends(advice_net, 
                   es = E(advice_net), # EDGES TO RETURN
                   names = FALSE # RETURN IDs, NOT NAMES
                   )[,1]         # RETURN ONLY THE STARTING NODE
edge.col <- V(advice_net)$color[edge.start]
par(mar = c(0, 0, 0, 0))
plot(advice_net, edge.color = edge.col, edge.curved = .1) 

Network Layouts

net.bg <- sample_pa(80)         # Barabasi-Albert model

V(net.bg)$size <- 8
V(net.bg)$frame.color <- "white"
V(net.bg)$color <- "orange"
V(net.bg)$label <- "" 
E(net.bg)$arrow.mode <- 0
plot(net.bg)

You can set the layout in the plot function:

plot(net.bg, layout=layout_randomly)

Or you can calculate the vertex coordinates in advance:

l <- layout_in_circle(net.bg)
plot(net.bg, layout=l)

l is simply a matrix of x, y coordinates (\(N \times 2\)) for the N nodes in the graph. You can easily generate your own:

l <- cbind(1:vcount(net.bg), c(1, vcount(net.bg):2))
l
##       [,1] [,2]
##  [1,]    1    1
##  [2,]    2   80
##  [3,]    3   79
##  [4,]    4   78
##  [5,]    5   77
##  [6,]    6   76
##  [7,]    7   75
##  [8,]    8   74
##  [9,]    9   73
## [10,]   10   72
## [11,]   11   71
## [12,]   12   70
## [13,]   13   69
## [14,]   14   68
## [15,]   15   67
## [16,]   16   66
## [17,]   17   65
## [18,]   18   64
## [19,]   19   63
## [20,]   20   62
## [21,]   21   61
## [22,]   22   60
## [23,]   23   59
## [24,]   24   58
## [25,]   25   57
## [26,]   26   56
## [27,]   27   55
## [28,]   28   54
## [29,]   29   53
## [30,]   30   52
## [31,]   31   51
## [32,]   32   50
## [33,]   33   49
## [34,]   34   48
## [35,]   35   47
## [36,]   36   46
## [37,]   37   45
## [38,]   38   44
## [39,]   39   43
## [40,]   40   42
## [41,]   41   41
## [42,]   42   40
## [43,]   43   39
## [44,]   44   38
## [45,]   45   37
## [46,]   46   36
## [47,]   47   35
## [48,]   48   34
## [49,]   49   33
## [50,]   50   32
## [51,]   51   31
## [52,]   52   30
## [53,]   53   29
## [54,]   54   28
## [55,]   55   27
## [56,]   56   26
## [57,]   57   25
## [58,]   58   24
## [59,]   59   23
## [60,]   60   22
## [61,]   61   21
## [62,]   62   20
## [63,]   63   19
## [64,]   64   18
## [65,]   65   17
## [66,]   66   16
## [67,]   67   15
## [68,]   68   14
## [69,]   69   13
## [70,]   70   12
## [71,]   71   11
## [72,]   72   10
## [73,]   73    9
## [74,]   74    8
## [75,]   75    7
## [76,]   76    6
## [77,]   77    5
## [78,]   78    4
## [79,]   79    3
## [80,]   80    2

l is simply a matrix of x, y coordinates (\(N \times 2\)) for the N nodes in the graph. You can easily generate your own:

l <- cbind(1:vcount(net.bg), c(1, vcount(net.bg):2))
plot(net.bg, layout=l)

igraph has a number of built-in layouts, including: Random

l <- layout_randomly(net.bg)
plot(net.bg, layout=l)

igraph has a number of built-in layouts, including: Circle

l <- layout_in_circle(net.bg)
plot(net.bg, layout=l)

igraph has a number of built-in layouts, including: Sphere

l <- layout_on_sphere(net.bg)
plot(net.bg, layout=l)

igraph has a number of built-in layouts, including: Fruchterman-Reingold

l <- layout_with_fr(net.bg)
plot(net.bg, layout=l)

igraph has a number of built-in layouts, including: Fruchterman-Reingold

Use rescale=FALSE to control the spread of the layout

l <- layout_with_fr(net.bg)
l <- norm_coords(l, ymin=-1, ymax=1, xmin=-1, xmax=1)

par(mfrow=c(1,4), mar=c(0,0,0,0))
plot(net.bg, rescale=F, layout=l*0.4)
plot(net.bg, rescale=F, layout=l*0.6)
plot(net.bg, rescale=F, layout=l*0.8)
plot(net.bg, rescale=F, layout=l*1.0)

Plot all the layouts:

layouts <- grep("^layout_", ls("package:igraph"), value=TRUE)[-1] 
# Remove layouts that do not apply to our graph.
layouts <- layouts[!grepl("bipartite|merge|norm|sugiyama|tree", layouts)]

par(mfrow=c(3,5), mar=c(1,1,1,1))
for (layout in layouts) {
  print(layout)
  l <- do.call(layout, list(advice_net)) 
  plot(advice_net, edge.arrow.mode=0, layout=l, main=layout) }

Assignment 4

  1. Identify the research question you will explore in your paper. Briefly explain why the question is worth answering.
  2. Identify the data you will use to test the question. Explain what the nodes and edges represent and how they are measured. Do not choose data you are not sure you can access.
  3. Briefly explain what network analysis methods you will use. Be clear about how the network analysis will help answer the research question
  • Limit your response to one dobule-spaced page.